package com.mhtool.json;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;

import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
 * @since 8.0.0
 */
@SuppressWarnings({"rawtypes" })
public abstract class TypeUtils {
    public static final TypeReference<Map<String, Object>> MAP = new TypeReference<Map<String, Object>>() {};
    /**
     * 两个泛型参数
     */
    public static JavaType mapLike(Class<?> mapClass, Class<?> keyClass, Class<?> valueClass) {
        return getTypeFactory().constructMapLikeType(mapClass, keyClass, valueClass);
    }

    /**
     * Map 泛型
     */
    public static JavaType map(Class<? extends Map> mapClass, Class<?> keyClass, Class<?> valueClass) {
        return getTypeFactory().constructMapType(mapClass, keyClass, valueClass);
    }

    /**
     * 1个泛型参数
     */
    public static JavaType listLike(Class<?> collectionClass, Class<?> elementClass) {
        return getTypeFactory().constructCollectionLikeType(collectionClass, elementClass);
    }

    /**
     * @return class of List<T>
     */
    public static JavaType list(Class<?> elementClass) {
        return list(List.class, elementClass);
    }

    /**
     * @return class of List<T>
     */
    public static JavaType list(Class<? extends Collection> collectionClass, Class<?> elementClass) {
        return getTypeFactory().constructCollectionType(collectionClass, elementClass);
    }

    public static JavaType constructType(TypeReference<?> typeRef) {
        return getTypeFactory().constructType(typeRef);
    }

    public static JavaType constructType(Class<?> clazz) {
        return getTypeFactory().constructType(clazz);
    }

    public static TypeFactory getTypeFactory() {
        return TypeFactory.defaultInstance();
    }
}
